package bam.dao.impl;


import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import bam.dao.StatisticsDao;
import bam.entityBam.Zzstatic;
import bam.service.RecordService;
import bam.util.PagingUtil;

public class StatisticsDaoImpl extends HibernateDaoSupport implements
		StatisticsDao {
	private RecordService recordService;
//	
	public void selectZzstatisList(PagingUtil<Zzstatic> pu, Zzstatic z,String odlDate,String newDate)
			throws Exception {
		pu.setLogCount(scount( z,odlDate,newDate));
		
		String sql = "select * from zzstatic where " +
				"area_id =  ? and " +
				"set_time > ? and " +
				"set_time < ? and " +
				"day_user > ? and " +
				"new_user > ? and " +
				"day_net  > ? and " +
				"new_binding > ? order by id desc";
		Query query = this.getSession().createSQLQuery(sql).addEntity( Zzstatic.class);	
		query.setParameter(0, z.getAreaId());
		query.setParameter(1, odlDate);
		query.setParameter(2, newDate);
		query.setParameter(3, z.getDayUser());
		query.setParameter(4, z.getNewUser());
		query.setParameter(5, z.getDayNet());
		query.setParameter(6, z.getNewBinding());
		int start=(pu.getPageNo()-1)*pu.getPageSize();
		int size = pu.getPageSize();
		query.setFirstResult(start);
		query.setMaxResults(size);
		List<Zzstatic> list = query.list();
		pu.setList(list);
	}
	
	private int scount(Zzstatic z,String odlDate,String newDate){
		System.out.println(odlDate);
		System.out.println(newDate);
		String sql = "select count(id) from zzstatic where " +
				"area_id  =  ? and " +
				"set_time > ? and " +
				"set_time < ? and " +
				"day_user > ? and " +
				"new_user > ? and " +
				"day_net  > ? and " +
				"new_binding > ?";
		
		Query query = this.getSession().createSQLQuery(sql);	
		query.setParameter(0, z.getAreaId());
		query.setParameter(1, odlDate);
		query.setParameter(2, newDate);
		query.setParameter(3, z.getDayUser());
		query.setParameter(4, z.getNewUser());
		query.setParameter(5, z.getDayNet());
		query.setParameter(6, z.getNewBinding());
		
		String uniqueResult = query.uniqueResult().toString();
		System.out.println(uniqueResult);
		System.out.println("+++++++++++++++++++++++++++"+uniqueResult);
		return Integer.parseInt(uniqueResult);
		
	}
//	
	
	public Serializable insertZzstatic(Zzstatic zzstatic) throws Exception {
		return this.getHibernateTemplate().save(zzstatic);
	}
//
	public Zzstatic selectzzstatic(int areaid, String newDate, String oldDate) throws Exception {
		
		int dayUser = recordService.selectDayUser(areaid+"", newDate, oldDate);
		int newBinding = recordService.selectNewBinding(areaid, newDate, oldDate);
		int newNet = recordService.selectNewNet(areaid, newDate, oldDate);
		int newUser = recordService.selectNewUser(areaid+"", newDate, oldDate);
		int countBinding = recordService.selectcountBinding(areaid, newDate);
		int countUser =recordService.selectcountUser(areaid, newDate);
		Zzstatic zzstatic = new Zzstatic(null, areaid, dayUser, newUser, newNet, newBinding, countUser, countBinding);
		return zzstatic;
	}
//
	public void updatezzstatic(Zzstatic zzstatic) throws Exception {
			this.getHibernateTemplate().update(zzstatic);
	}
//
	public Zzstatic selectzzstaticOne(int areaid, String newDate, String oldDate)
			throws Exception {
		String sql = " from Zzstatic where " +
				"area_id  =  ? and " +
				"set_time > ? and " +
				"set_time < ? ";
		Query query = this.getSession().createQuery(sql);
		query.setParameter(0, areaid);
		query.setParameter(1, oldDate);
		query.setParameter(2, newDate);
		List<Zzstatic> list = query.list();
		return list.isEmpty()?null:list.get(0);
	}

	public void setRecordService(RecordService recordService) {
		this.recordService = recordService;
	}


}
